package com.um.jdy.ykc.command;

import cn.hutool.core.util.ArrayUtil;
import com.um.jdy.ykc.utils.RadixsUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

import java.util.List;

/**
 * 计费模型请求应答（运营平台->充电桩）
 *
 * @author 甘天伟
 * @since 2022/6/27
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class Cmd0AGetPriceModelResponse extends YkcCommand {

    /**
     * 桩编码：7个字节
     */
    private String code;

    /**
     * 计费模型编号：2个字节
     */
    private String feeMode;

    /**
     * 尖电费费率：4个字节
     */
    private double sharpFee;

    /**
     * 尖服务费费率：4个字节
     */
    private double sharpServiceFee;

    /**
     * 峰电费费率：4个字节
     */
    private double peakFee;

    /**
     * 峰服务费费率：4个字节
     */
    private double peakServiceFee;

    /**
     * 平电费费率：4个字节
     */
    private double shoulderFee;

    /**
     * 平服务费费率：4个字节
     */
    private double shoulderServiceFee;

    /**
     * 谷电费费率：4个字节
     */
    private double offPeakFee;

    /**
     * 谷服务费费率：4个字节
     */
    private double offPeakServiceFee;

    /**
     * 计损比例：1个字节
     */
    private int lossScale;

    /**
     * 48个时段费率号：48个字节 0x00:尖费率0x01:峰费率0x02: 平费率 0x03:谷费率
     */
    private List<Integer> time48Fee;

    public Cmd0AGetPriceModelResponse(int serial, String code, String feeMode, double sharpFee, double sharpServiceFee, double peakFee, double peakServiceFee
            , double shoulderFee, double shoulderServiceFee, double offPeakFee, double offPeakServiceFee, int lossScale, List<Integer> time48Fee) {
        super();

        this.serial = serial;
        this.encFlag = RadixsUtil.hexToInteger("00");
        this.command = RadixsUtil.hexToInteger("0A");

        this.code = code;
        this.feeMode = feeMode;
        this.sharpFee = sharpFee;
        this.sharpServiceFee = sharpServiceFee;
        this.peakFee = peakFee;
        this.peakServiceFee = peakServiceFee;
        this.shoulderFee = shoulderFee;
        this.shoulderServiceFee = shoulderServiceFee;
        this.offPeakFee = offPeakFee;
        this.offPeakServiceFee = offPeakServiceFee;
        this.lossScale = lossScale;
        this.time48Fee = time48Fee;

        byte[] _code = RadixsUtil.hexToBytes(code);
        byte[] _feeMode = RadixsUtil.hexToBytes(feeMode);
        byte[] _sharpFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(sharpFee, 5));
        byte[] _sharpServiceFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(sharpServiceFee, 5));
        byte[] _peakFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(peakFee, 5));
        byte[] _peakServiceFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(peakServiceFee, 5));
        byte[] _shoulderFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(shoulderFee, 5));
        byte[] _shoulderServiceFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(shoulderServiceFee, 5));
        byte[] _offPeakFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(offPeakFee, 5));
        byte[] _offPeakServiceFee = ArrayUtil.reverse(RadixsUtil.double32ToBytes(offPeakServiceFee, 5));
        byte _lossScale = (byte) lossScale;
        byte[] _time48Fee = new byte[48];

        for (int index = 0; index < time48Fee.size(); index++) {
            _time48Fee[index] = time48Fee.get(index).byteValue();
        }

        ByteBuf buff = Unpooled.buffer();
        buff.writeBytes(fillZeroBytes(_code, 7 - _code.length));
        buff.writeBytes(fillZeroBytes(_feeMode, 2 - _feeMode.length));
        buff.writeBytes(_sharpFee);
        buff.writeBytes(_sharpServiceFee);
        buff.writeBytes(_peakFee);
        buff.writeBytes(_peakServiceFee);
        buff.writeBytes(_shoulderFee);
        buff.writeBytes(_shoulderServiceFee);
        buff.writeBytes(_offPeakFee);
        buff.writeBytes(_offPeakServiceFee);
        buff.writeByte(_lossScale);
        buff.writeBytes(_time48Fee);

        this.data = new byte[buff.readableBytes()];
        buff.readBytes(data);
        buff.release();

        postConstruct();
    }
}
